//package com.basin.common.glc.filter;
//
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.util.StrUtil;
//import com.basin.common.glc.wrapper.CachedBodyHttpServletRequest;
//import com.basin.common.glc.wrapper.CachedBodyHttpServletResponse;
//import com.basin.common.satoken.support.BasinAuthTool;
//import jakarta.annotation.PostConstruct;
//import jakarta.servlet.*;
//import jakarta.servlet.annotation.WebFilter;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//import java.nio.charset.StandardCharsets;
//import java.util.Date;
//
/// **
// * @author: Sam ZHONG | sammy1997@aliyun.com
// * @date: 2025/6/9 19:22
// * @description:
// */
//@Component
//@Order(-99)
//@Slf4j
//@WebFilter(urlPatterns = "/*", filterName = "requestLoggingFilter")
//public class SysReqLogFilter implements Filter {
//    private static final String LOG_TEMPLATE = """
//            \n=======BASIN-SYS-LOG=======
//            ClientIP: %s
//            RequestUrl: %s
//            RequestMethod: %s
//            ContentType: %s
//            RequestParams: %s
//            ResponseData: %s
//            OperatorUserId: %s
//            TenantId: %s
//            occurrenceTime: %s
//            TimeCost: %sms
//            =======BASIN-SYS-LOG=======""";
//
//    @PostConstruct
//    public void init() {
//        log.info("Basin SysReqLogFilter init successfully");
//    }
//
//    @Override
//    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
//        long startTime = System.currentTimeMillis();
//        CachedBodyHttpServletRequest cachedReq = new CachedBodyHttpServletRequest((HttpServletRequest) req);
//        CachedBodyHttpServletResponse cachedRes = new CachedBodyHttpServletResponse((HttpServletResponse) res);
//        chain.doFilter(cachedReq, cachedRes);
//        long duration = System.currentTimeMillis() - startTime;
//        String url = cachedReq.getRequestURL().toString();
//        String ip = cachedReq.getRemoteAddr();
//        String method = cachedReq.getMethod();
//        String contentType = cachedReq.getContentType();
//        String queryString = cachedReq.getQueryString();
//        String requestParams = getRequestParams(cachedReq);
//        if (StrUtil.isNotBlank(queryString)) {
//            url = url + "?" + queryString;
//        }
//        String loginId = "-";
//        try {
//            loginId = BasinAuthTool.getLoginId();
//        } catch (Exception e) {
//            // 无事发生
//        }
//        String tenantId = "-";
//        try {
//            tenantId = BasinAuthTool.getTenantId();
//        } catch (Exception e) {
//            // 无事发生
//        }
//        // todo 文件下载的话这里得额外处理
//        String responseBody = new String(cachedRes.getResponseBody(), StandardCharsets.UTF_8);
//        String logT = LOG_TEMPLATE.formatted(ip, url, method, contentType,
//                requestParams, responseBody,
//                loginId, tenantId, DateUtil.format(new Date(startTime), "yyyy-MM-dd HH:mm:ss.SSS"), duration);
//        log.info(logT);
//
//        // 写回去 不然客户端收不到数据
//        res.getOutputStream().write(responseBody.getBytes());
//        res.getOutputStream().flush();
//    }
//
//    private String getRequestParams(CachedBodyHttpServletRequest request) {
//        String rbd = new String(request.cachedBody, StandardCharsets.UTF_8);
//        if (StrUtil.isNotBlank(rbd)) {
//            rbd = rbd.replace(" ", "");
//        }
//        return rbd;
//    }
//}
